package com.zero.business.member.service.impl;

import com.zero.domain.Member;
import com.zero.business.member.mapper.MemberMapper;
import com.zero.business.member.service.MemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service
public class MemberServiceImpl implements MemberService{

    @Resource
    private MemberMapper memberMapper;

    @Override
    public int deleteByPrimaryKey(Integer userId) {
        return memberMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public int insert(Member record) {
        return memberMapper.insert(record);
    }

    @Override
    public int insertSelective(Member record) {
        return memberMapper.insertSelective(record);
    }

    @Override
    public Member selectByPrimaryKey(Integer userId) {
        return memberMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updateByPrimaryKeySelective(Member record) {
        return memberMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Member record) {
        return memberMapper.updateByPrimaryKey(record);
    }

    /*
    *   会员充值
    * */
    @Override
    public int recharge(Integer userId, BigDecimal money) {
        // 根据 userId 查询用户
        Member member = memberMapper.selectByPrimaryKey(userId);
        // 更新金额      = 原来的金额 + 充值金额
        member.setMoney(member.getMoney().add(money));

        // 更新人
        member.setUpdateBy("admin");
        // 更新时间
        member.setUpdateTime(new Date());

        // 更新
        return memberMapper.updateByPrimaryKeySelective(member);
    }

}
